package com.lianyi.push.share.channelinitializer;

import android.content.Context;

import com.lianyi.push.share.listener.TcpSeiviceToClientMessageListener;
import com.lianyi.push.share.tcpclient.TcpClientHandler;

import java.util.concurrent.TimeUnit;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * Created by leilei on 2018/8/17.
 */

public class TcpClientChannelInitializer extends ChannelInitializer<SocketChannel> {
    private Context mContext;
    private TcpSeiviceToClientMessageListener tcpSeiviceToClientMessageListener;

    public TcpClientChannelInitializer(Context context, TcpSeiviceToClientMessageListener listener) {
        this.mContext = context;
        this.tcpSeiviceToClientMessageListener = listener;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
//        ch.pipeline().addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS));
        ch.pipeline().addLast(new ObjectDecoder(1024 * 1024 * 1024,
                ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
        ch.pipeline().addLast(new ObjectEncoder());
        ch.pipeline().addLast(new TcpClientHandler(mContext, tcpSeiviceToClientMessageListener));
    }
}
